
package com.haisen.config;

import com.haisen.core.shiro.spring.SpringCacheManagerWrapper;
import net.sf.ehcache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/**
 * @author cjbi
 * 缓存实现类,注意用户shiro登录信息的缓存
 */

@Configuration
@EnableCaching
public class EhCacheConfig {

     /**
     * 缓存管理器的包装集
     *EhCacheCacheManager--实现了继承了抽象类,实现了接口org.springframework.cache.CacheManager
      * 可以setCacheManager
     * @return
     */

    /**
     * core.shiro.SpringCacheManager类
     * @param springCacheManager
     * @return
     */
    @Bean
    public SpringCacheManagerWrapper cacheManager(EhCacheCacheManager springCacheManager) {
        //cache管理器的封装
        SpringCacheManagerWrapper cacheManager = new SpringCacheManagerWrapper();
        cacheManager.setCacheManager(springCacheManager);
        return cacheManager;
    }

    /**
     * EhCache缓存管理器,可以注入到SpringCacheMangerWrapper中
     */

    @Bean
    public EhCacheCacheManager springCacheManager(CacheManager cacheManager) {
        return new EhCacheCacheManager(cacheManager);
    }

    /**
     * EhCache的配置
     * 有你
     */

    @Bean
    public EhCacheManagerFactoryBean ehcacheManager() {
        EhCacheManagerFactoryBean ehCacheManagerFactoryBean = new EhCacheManagerFactoryBean();
        Resource resource = new ClassPathResource("ehcache.xml");
        ehCacheManagerFactoryBean.setConfigLocation(resource);
        return ehCacheManagerFactoryBean;
    }

}

